<?php

class __admin_system_rss extends Module {
	
	public function __construct() {
		parent::__construct('system');
	}
	
	public function rss() {
		$this->setPageTitle(lang('admin_header_rss','system'));
		
		$typesController = TypesController::getInstance();
		
		$oTypeRss = $typesController->getTypeByName('rss_type');
		
		$sel = new Selection;
		$sel->addType($oTypeRss);
		
		$aObjects = ObjectsController::getInstance()->getObjectsList($sel->getList());
		
		$response = new ModuleResponse;
		$response->setTemplate('system/rss/list');
		
		if (getRequest('created','bool')) {
			$response->addObject(new SystemMessage(lang('admin_rss_created','system')));
		}
		if (getRequest('saved','bool')) {
			$response->addObject(new SystemMessage(lang('admin_rss_saved','system')));
		}
		if (getRequest('deleted','bool')) {
			$response->addObject(new SystemMessage(lang('admin_rss_deleted','system')));
		}
		
		foreach ($aObjects as $object) {
			$object->setAttribute('rss-type-title', $typesController->getTypeById($object->getValue('type_id'))->getTitle());
			$response->addObject($object);
		}
		
		return $response;
	}
	
	public function add_rss() {
		return $this->form();
	}
	
	public function edit_rss() {
		return $this->form(getRequest('param0','int'));
	}
	
	public function delete_rss() {
		$this->expectSecureGET();
		
		if (!$object = ObjectsController::getInstance()->getObject(TypesController::getInstance()->getTypeByName ('rss_type'), getRequest('param0','int'))) {
			throw new NotFound;
		}
		
		$object->delete();
		
		redirect(HTTP_HOST . '/admin/system/rss?deleted');
	}
	
	private function form($id = 'new') {
		$oTypeRss = TypesController::getInstance()->getTypeByName ('rss_type');
		
		$form = new Form ($oTypeRss, $id);
		$form->buildFromType ();
		
		$aValues = array ();
		$aErrors = array ();
		
		if ($id != 'new') {
			if (!$object = ObjectsController::getInstance()->getObject ($oTypeRss, $id)) {
				throw new NotFound;
			}
			
			$aValues = $object->getValues ();
		}
		
		if (($id == 'new' && getRequest ('param0') == 'save') || ($id != 'new' && getRequest ('param1') == 'save')) {
			$aValues = $this->getRequestData ($id, $oTypeRss);
			
			if (isset($aValues['image_thumb_size']) && $aValues['image_thumb_size'] == '') {
				$aValues['image_thumb_size'] = null;
			}
			if (isset($aValues['image_field']) && !$aValues['image_field']) {
				$aValues['image_field'] = null;
			}
			if (isset($aValues['tags_field']) && !$aValues['tags_field']) {
				$aValues['tags_field'] = null;
			}
			if (isset($aValues['pubdate_field']) && !$aValues['pubdate_field']) {
				$aValues['pubdate_field'] = null;
			}
			if (isset($aValues['owner_name_field']) && !$aValues['owner_name_field']) {
				$aValues['owner_name_field'] = null;
			}

			if ($id == 'new') {
				$object = new Object ($oTypeRss);
				$object->setValues ($aValues);
			} else {
				$object->setValues ($aValues);
			}
				
			$aErrors = $this->getValidateErrors ($object);
			
			if (!isset($aErrors['type_id'])) {
				$sel = new Selection;
				$sel->addType($oTypeRss);
				$sel->addFilter('type_id', $object->getValue('type_id'));
				if ($id != 'new') {
					$sel->addFilter('id', $id, '!=');
				}
				
				if ($sel->getTotalCount()) {
					$aErrors['type_id'] = lang('error_admin_rss_type_exists','system');
				}
			}
			
			if (!count ($aErrors)) {
				if ($id == 'new') {
					$object->createNew ();
					redirect (HTTP_HOST . '/admin/system/rss?created');
				} else {
					$object->save ();
					redirect (HTTP_HOST . '/admin/system/rss?saved');
				}
			}
		}
		
		$form->setErrors ($aErrors);
		$form->setValues ($aValues);
		
		$response = new ModuleResponse;
		$response->setTemplate ('system/rss/form');
		$response->addObject ($form);
		
		return $response; 
	}
	
}